package com.lin.manager.common.user;

/**
 * @作者: 林江
 * @创建时间: 2024/3/20
 * @功能:
 */
public class UserInfoContextHolder {
    private static ThreadLocal<UserInfoContext> userInfoContextThreadLocal = new ThreadLocal<>();

    public static void setUserInfo(UserInfoContext userInfo) {
        userInfoContextThreadLocal.set(userInfo);
    }

    public static Long getUserId() {
        UserInfoContext userInfoContext = userInfoContextThreadLocal.get();
        if (userInfoContext != null) {
            return userInfoContext.getUserId();
        }
        return null;
    }

    public static String getUserName() {
        UserInfoContext userInfoContext = userInfoContextThreadLocal.get();
        if (userInfoContext != null) {
            return userInfoContext.getUsername();
        }
        return "";
    }

    public static UserInfoContext getUserInfo() {
        return userInfoContextThreadLocal.get();
    }

    public static void clear() {
        userInfoContextThreadLocal.remove();
    }
}
